package com.nian.dao.impl;

import com.nian.dao.BaseDao;
import com.nian.dao.ProductDao;
import com.nian.pojo.Product;

import java.util.List;

public class ProductDaoImpl extends BaseDao implements ProductDao {

    @Override
    public boolean addProduct(Product product) {
        boolean result = false;
        //"insert into `user`(`u_name`,`u_pwd`,`u_phone`,`u_realname`)VALUES(?,?,?,?) "
        String sql ="insert into `t_product`(`id`,`name`,`price`,`category`,`pnum`,`imgurl`,`description`)values(?,?,?,?,?,?,?)";
        int count = update(sql,product.getId(), product.getName(), product.getPrice(), product.getCategory(), product.getpNum(), product.getImgUrl(), product.getDescription());
        if (count>0){
            result = true;
        }
        return result;
    }

    @Override
    public boolean dlProduct(Product product) {
        boolean result =false;
        ////"DELETE FROM `house` WHERE `h_id`= ?";
        String sql = "DELETE FROM `t_product` WHERE `id` = ?";
        int count = update(sql, product.getId());
        if (count>0){
            result = true;
        }
        return result;
    }

    @Override
    public boolean reProduct(Product product) {
        boolean result = false;
        //UPDATE `T_user` SET
        String sql = "UPDATE `t_product` SET `name`= ? ,`price` = ?,`category` = ?,`pnum` = ?,`imgurl`= ?,`description` = ? WHERE id = ?";
        int count = update(sql, product.getName(), product.getPrice(), product.getCategory(), product.getpNum(), product.getImgUrl(), product.getDescription(),product.getId());
        if (count>0){
            result = true;
        }
        return result;
    }

    @Override
    public Product selectProductById(Product product) {
        //String sql = "select * from `t_user` where `id` = ?";
        String sql = "select * from `t_product` where id = ?";
        return queryForOne(Product.class,sql,product.getId());
    }

    @Override
    public Product selectProductByName(Product product) {
        String sql = "select * from `t_product` where name = ?" ;
        return queryForOne(Product.class,sql,product.getName());
    }

    @Override
    public List<Product> selectAllProduct() {
        String sql = "select * from `t_product`";
        return queryForList(Product.class,sql);
    }
}
